package com.usian.controller;

import com.usian.pojo.Category;
import com.usian.service.CategoryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class CategoryController {
    private Logger logger= LoggerFactory.getLogger(HelloController.class);
    @Autowired
    private CategoryService categoryService;


    //查询全部
    @RequestMapping("findAll")
    public String findAll(Model model) {
        logger.info("进入了findAll------");
        try {
            List<Category> dates = categoryService.findAll();
            model.addAttribute("list", dates);
        } catch (Exception e) {
            logger.error("findAll出现了异常------"+e.getMessage());
        }
        logger.info("退出了findAll-------");
        return "list";
    }

    //添加

    @RequestMapping("add")
    public String add(Category category) {
        categoryService.add(category);
        return "redirect:/findAll";
    }

    //删除

    @RequestMapping("del")
    public String del(Integer id) {
        categoryService.del(id);
        return "redirect:/findAll";
    }

    //修改
    @RequestMapping("update")
    public String update(Category category) {
        categoryService.update(category);
        return "redirect:/findAll";
    }


    //根据品牌Id回显
    @RequestMapping("findById")
    public String findById(Model model, Integer id){
        Category category=categoryService.findById(id);
        model.addAttribute("c",category);
        return "update";
    }

    //    @RequestMapping("findById")
//    public String findById(Integer id, Model model) {
//        Category category = bandService.findById(id);
//        model.addAttribute("category", category);
//        return "categoryId";
//    }



//    @RequestMapping("edit")
//    public String edit (Category category){
//        bandService.edit(category);
//        return "forward:findAll";
//    }

//    @RequestMapping("add")
//    public String add (Category category){
//        bandService.add(category);
//        return "redirect:findAll";
//    }
}
